package com.company.bookstore.common.formatUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.bookstore.entities.book_module.Book;

/**
 * <p>
 * URL拼接工具类
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-20
 */
public class FormatUtils {

    /**
     * 为一个book对象包含的所有ImgName拼接URL
     *
     * @param book
     * @return
     */
    public static Book formatOneImgUrl(Book book) {
        for (int i = 0; i < book.getBookImgs().size(); i++) {
            String imgName = book.getBookImgs().get(i).getImgName();
            if (imgName.startsWith("https://bookstore-img.oss-cn-shenzhen.aliyuncs.com/")){
                continue;
            }else{
                book.getBookImgs().get(i).setImgName("https://bookstore-img.oss-cn-shenzhen.aliyuncs.com/" + book.getBookImgs().get(i).getImgName());
            }
        }
        return book;
    }

    /**
     * 为一个books列表中的所有book的所有ImgName拼接URL
     *
     * @param books
     * @return
     */
    public static IPage<Book> formatManyImgUrl(IPage<Book> books) {
        for (int i = 0; i < books.getRecords().size(); i++) {
            formatOneImgUrl(books.getRecords().get(i));
        }
        return books;
    }
}
